import turtle
import math

# 全局常量设置
SCREEN_SIZE = 800  # 窗口尺寸
TRIANGLE_SIDE = 300  # 三角形显示边长 (像素)
REAL_SIDE = 100  # 实际边长 (米)
SPEED_RATIO = 0.5  # 显示速度与实际速度比例

# 计算实际坐标系参数
h_real = (math.sqrt(3) / 2) * REAL_SIDE  # 实际三角形高度
center_to_vertex = h_real * 2 / 3  # 中心到顶点实际距离 (57.735米)
center_to_edge = h_real / 3  # 中心到边缘实际距离 (28.867米)

# 初始化窗口
screen = turtle.Screen()
screen.setup(SCREEN_SIZE, SCREEN_SIZE)
screen.title("鳄鱼围捕逃脱模拟")


# 创建等边三角形边框
def draw_triangle():
    h_display = (math.sqrt(3) / 2) * TRIANGLE_SIDE  # 显示高度
    ratio = TRIANGLE_SIDE / REAL_SIDE  # 像素/米比例

    drawer = turtle.Turtle()
    drawer.hideturtle()
    drawer.penup()
    drawer.goto(-TRIANGLE_SIDE / 2, -h_display / 3)  # 左下顶点
    drawer.pendown()
    drawer.goto(0, 2 * h_display / 3)  # 顶部顶点
    drawer.goto(TRIANGLE_SIDE / 2, -h_display / 3)  # 右下顶点
    drawer.goto(-TRIANGLE_SIDE / 2, -h_display / 3)
    drawer.penup()


draw_triangle()


# 创建游戏角色
class Character(turtle.Turtle):
    def __init__(self, color, shape='circle'):
        super().__init__(shape=shape)
        self.color(color)
        self.penup()
        self.speed(0)


# 初始化所有角色
human = Character('green')  # 人类
croc_top = Character('red')  # 顶部鳄鱼
croc_left = Character('blue')  # 左侧鳄鱼
croc_right = Character('orange')  # 右侧鳄鱼

# 设置初始位置 (像素坐标系)
ratio = TRIANGLE_SIDE / REAL_SIDE  # 像素/米换算比例


def set_real_pos(obj, x_meter, y_meter):
    obj.goto(x_meter * ratio, y_meter * ratio)


set_real_pos(human, 0, 0)
set_real_pos(croc_top, 0, center_to_vertex)
set_real_pos(croc_left, -REAL_SIDE / 2, -center_to_edge)
set_real_pos(croc_right, REAL_SIDE / 2, -center_to_edge)

# 运动参数配置
SPEEDS = {
    'human': 10 * SPEED_RATIO,  # 人类速度 (像素/帧)
    'top': 20 * SPEED_RATIO,  # 顶部鳄鱼
    'left': 20 * SPEED_RATIO,  # 左侧鳄鱼
    'right': 15 * SPEED_RATIO  # 右侧鳄鱼
}


# 最优路径计算函数
def calculate_escape_angle():
    """通过向量分析计算最佳逃脱角度"""
    # 优先考虑最慢的右侧鳄鱼(15m/s)方向
    target_angle = math.radians(30)  # 经验最佳角度
    return target_angle


# 主模拟循环
def main_simulation():
    escape_angle = calculate_escape_angle()
    human.showturtle()

    while True:
        # 人类移动
        human_x = human.xcor() + SPEEDS['human'] * math.cos(escape_angle)
        human_y = human.ycor() + SPEEDS['human'] * math.sin(escape_angle)
        human.goto(human_x, human_y)

        # 检查是否逃脱 (到达边缘)
        if math.hypot(human_x, human_y) >= center_to_edge * ratio:
            human.write("逃脱成功!", align='center', font=('Arial', 16, 'bold'))
            return

        # 鳄鱼移动（实时追踪）
        for croc, name in [(croc_top, 'top'), (croc_left, 'left'), (croc_right, 'right')]:
            dx = human_x - croc.xcor()
            dy = human_y - croc.ycor()
            distance = math.hypot(dx, dy)

            if distance > 0:  # 防止除零错误
                croc_x = croc.xcor() + dx / distance * SPEEDS[name]
                croc_y = croc.ycor() + dy / distance * SPEEDS[name]
                croc.goto(croc_x, croc_y)

                # 检查是否被抓
                if math.hypot(human_x - croc_x, human_y - croc_y) < 5:  # 5像素捕获半径
                    human.color('red')
                    human.write("被捕!", align='center', font=('Arial', 16, 'bold'))
                    return


# 运行模拟
main_simulation()
turtle.done()